package com.tcmj.dining.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tcmj.common.annotation.Log;
import com.tcmj.common.enums.BusinessType;
import com.tcmj.dining.domain.TzDiningRoom;
import com.tcmj.dining.domain.TzVagetables;
import com.tcmj.dining.domain.TzVagetablesOrder;
import com.tcmj.dining.service.ITzDiningRoomService;
import com.tcmj.dining.service.ITzVagetablesOrderService;
import com.tcmj.dining.service.ITzVagetablesService;
import com.tcmj.dining.utils.SysUtils;
import com.tcmj.framework.util.ShiroUtils;
import com.tcmj.system.domain.SysUser;
import com.tcmj.system.service.ISysUserService;
import com.tcmj.common.core.controller.BaseController;
import com.tcmj.common.core.domain.AjaxResult;
import com.tcmj.common.utils.poi.ExcelUtil;
import com.tcmj.common.core.page.TableDataInfo;

/**
 * 点餐Controller
 * 
 * @author tcmj
 * @date 2020-05-17
 */
@Controller
@RequestMapping("/system/order")
public class TzVagetablesOrderController extends BaseController
{
    private String prefix = "system/order";

    @Autowired
    private ITzVagetablesOrderService tzVagetablesOrderService;
    
    @Autowired
    private ITzVagetablesService tzVagetablesService;
    
    @Autowired
    private ITzDiningRoomService tzDiningRoomService;
    
    @Autowired
    private ISysUserService sysUserService;
    
    private AjaxResult ajaxResult = null;

    @RequiresPermissions("system:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }
    
    /**
     * 个人订餐列表
     */
    @RequiresPermissions("system:order:diningView")
    @GetMapping("/diningView")
    public String diningView(TzVagetables tzVagetables,ModelMap mmap)
    {
    	//初始化  页面大小  当前页
    	tzVagetables.setPageSize(12);
    	if(tzVagetables.getPageNum()==0){
    		tzVagetables.setCurrentPageNum(1);
    	}else{
    		tzVagetables.setCurrentPageNum(tzVagetables.getPageNum());
    	}
    	
    	List<TzVagetables> list = tzVagetablesService.selectTzVagetablesList(tzVagetables);
    	
    	int total = tzVagetablesService.selectVagetablesCount();
    	
    	mmap.put("tzVagetabless", list);
    	
    	int totalPageNum = total/tzVagetables.getPageSize();
    	if(total % tzVagetables.getPageSize() != 0){
    		totalPageNum++;
    	}
    	//首页
    	if(tzVagetables.getCurrentPageNum()==1){
    		//隐藏上一页
    		tzVagetables.setHasPrevious(false);
    		tzVagetables.setHasNext(true);
    	}else if(tzVagetables.getCurrentPageNum()==totalPageNum){
    		//隐藏下一页
    		tzVagetables.setHasPrevious(true);
    		tzVagetables.setHasNext(false);
    	}
    	tzVagetables.setTotalPageNum(totalPageNum);
    	
    	Date date = new Date();//获取当前的日期
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
    	String str = df.format(date);//获取String类型的时间
    	String endTime = str + " 16:00:00";
    	mmap.put("endTime", endTime);
    	
    	int isDiningVagetables = 0;
    	//如果当前时间大于下午4点则返回超过订餐时间
    	if(SysUtils.isTimeRange()){
    		isDiningVagetables = 1;
    	}
    	mmap.put("isDiningVagetables", isDiningVagetables);
    	
    	TzDiningRoom tzDiningRoom = new TzDiningRoom();
    	List<TzDiningRoom> tzDiningRoomLists = tzDiningRoomService.selectTzDiningRoomList(tzDiningRoom);
    	
    	
    	mmap.put("tzDiningRooms", tzDiningRoomLists);
    	mmap.put("tzVagetables", tzVagetables);
        return prefix + "/diningView";
    }
    
    /**
     * 修改保存点餐
     */
    @Log(title = "点餐", businessType = BusinessType.UPDATE)
    @PostMapping("/diningSave")
    @ResponseBody
    public AjaxResult diningSave(TzVagetablesOrder tzVagetablesOrder)
    {
    	//如果当前时间大于下午4点则返回超过订餐时间
    	if(!SysUtils.isTimeRange()){
    		return error("订餐时间已过,请联系管理员");
    	}
    	
    	//获取当前登录用户
    	Long userId = ShiroUtils.getUserId();
    	tzVagetablesOrder.setUserId(userId);
    	tzVagetablesOrder.setOrderTime(new Date());
        return toAjax(tzVagetablesOrderService.insertTzVagetablesOrder(tzVagetablesOrder));
    }
    
    /**
     * 取消用户点餐
     */
    @PostMapping("/delOrderSubmit")
    @ResponseBody
    public AjaxResult delOrderSubmit()
    {
    	//如果当前时间大于下午4点则返回超过订餐时间
    	if(!SysUtils.isTimeRange()){
    		return error("取消订餐时间已过,请联系管理员");
    	}
    	//获取当前登录用户
    	Long userId = ShiroUtils.getUserId();
    	int n = tzVagetablesOrderService.deleteTzVagetablesOrderByUserId(userId);
    	if(n>0){
    		ajaxResult = AjaxResult.success("取消订餐成功");
    	}else{
    		ajaxResult = AjaxResult.error("取消订餐失败");
    	}
        return ajaxResult;
    }
    
    /**
     * 用户是否订餐
     */
    @PostMapping("/isDiningVagetables")
    @ResponseBody
    public AjaxResult isDiningVagetables()
    {
    	String vagetablesName = "";
    	//获取当前登录用户
    	Long userId = ShiroUtils.getUserId();
    	//根据当前用户查询当天的订单
    	List<TzVagetablesOrder> tzVagetablesOrderList = tzVagetablesOrderService.selectTzVagetablesByUserId(userId);
    	if(tzVagetablesOrderList.size()>0){
    		vagetablesName = tzVagetablesOrderList.get(0).getTzVagetables().getVagetablesName();
    	}
        return success(vagetablesName);
    }
    
    /**
     * 查询点餐列表
     */
    @RequiresPermissions("system:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TzVagetablesOrder tzVagetablesOrder)
    {
        startPage();
        List<TzVagetablesOrder> list = tzVagetablesOrderService.selectTzVagetablesOrderList(tzVagetablesOrder);
        return getDataTable(list);
    }

    /**
     * 导出点餐列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "点餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TzVagetablesOrder tzVagetablesOrder)
    {
        List<TzVagetablesOrder> list = tzVagetablesOrderService.selectTzVagetablesOrderList(tzVagetablesOrder);
        ExcelUtil<TzVagetablesOrder> util = new ExcelUtil<TzVagetablesOrder>(TzVagetablesOrder.class);
        for(TzVagetablesOrder tzVagetablesOrders :list){
        	tzVagetablesOrders.setVagetablesName(tzVagetablesOrders.getTzVagetables().getVagetablesName());
        	tzVagetablesOrders.setUserName(tzVagetablesOrders.getSysUser().getUserName());
        }
        return util.exportExcel(list, "order");
    }

    /**
     * 新增点餐
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	mmap.put("tzVagetabless", tzVagetablesService.selectTzVagetablesList(new TzVagetables()));
    	mmap.put("sysUsers", sysUserService.selectUserList(new SysUser()));
        return prefix + "/add";
    }

    /**
     * 新增保存点餐
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "点餐", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TzVagetablesOrder tzVagetablesOrder)
    {
        return toAjax(tzVagetablesOrderService.insertTzVagetablesOrder(tzVagetablesOrder));
    }

    /**
     * 修改点餐
     */
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        TzVagetablesOrder tzVagetablesOrder = tzVagetablesOrderService.selectTzVagetablesOrderById(orderId);
        mmap.put("tzVagetablesOrder", tzVagetablesOrder);
        
        mmap.put("tzVagetabless", tzVagetablesService.selectTzVagetablesList(new TzVagetables()));
    	mmap.put("sysUsers", sysUserService.selectUserList(new SysUser()));
        return prefix + "/edit";
    }

    /**
     * 修改保存点餐
     */
    @RequiresPermissions("system:order:edit")
    @Log(title = "点餐", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TzVagetablesOrder tzVagetablesOrder)
    {
        return toAjax(tzVagetablesOrderService.updateTzVagetablesOrder(tzVagetablesOrder));
    }

    /**
     * 删除点餐
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "点餐", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tzVagetablesOrderService.deleteTzVagetablesOrderByIds(ids));
    }
}
